package com.watson.activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Base64;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.watson.BaseActivity;
import com.watson.R;
import com.watson.adapter.HomeAdapter;
import com.watson.adapter.SourceZAdapter;
import com.watson.bean.ChooseBean;
import com.watson.http.CommonHttpConsts;
import com.watson.http.HttpCallbackWdss;
import com.watson.http.HttpClient;
import com.watson.utils.DialogUitl;
import com.watson.utils.LogUtils;
import com.watson.utils.SPUtils;
import com.watson.utils.ToastUtil;

import java.util.ArrayList;
import java.util.Map;

public class HistoryActivity extends BaseActivity {
    private XRecyclerView mRecyclerview;
    private HomeAdapter historyAdapter;
    private ArrayList<Map> dataList = new ArrayList<>();
    private int page = 1;
    private TextView tvTitle, tvRight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);

        statusbar4();
        findViewById(R.id.imgBack).setOnClickListener(v -> finish());
        mRecyclerview = findViewById(R.id.mXRecyclerView);
        tvTitle = findViewById(R.id.tvTitle);
        tvRight = findViewById(R.id.tvRight);
        tvRight.setText("筛选");
        tvRight.setVisibility(View.GONE);
        tvTitle.setText("历史识别列表");

        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext, RecyclerView.VERTICAL, false);
        mRecyclerview.setLayoutManager(layoutManager);
        mRecyclerview.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        mRecyclerview.setLoadingMoreProgressStyle(ProgressStyle.BallClipRotate);
        historyAdapter = new HomeAdapter(mContext, dataList);
        mRecyclerview.setAdapter(historyAdapter);
        historyAdapter.setItemClickListener((position, mList) -> {
            getHistoryById(mList.get(position).get("id") + "");
        });

        mRecyclerview.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page = 1;
                GetRegHistories();
            }

            @Override
            public void onLoadMore() {
                mRecyclerview.refreshComplete();
                mRecyclerview.setPullRefreshEnabled(true);
            }
        });

        mRecyclerview.refresh();
        mRecyclerview.refreshComplete();
        mRecyclerview.setPullRefreshEnabled(true);

        stateList = new ArrayList<>();
        for (int i = 0; i < state.length; i++) {
            ChooseBean chooseBean = new ChooseBean();
            chooseBean.setId(i);
            chooseBean.setName(state[i]);
            stateList.add(chooseBean);
        }
        stateList.get(0).setType(-1);

        tvRight.setOnClickListener(v -> {
            dismiss();
            initPopupWindow(tvRight, stateList);
        });
    }

    private void GetRegHistories() {}

    private PopupWindow popupWindow;
    private ArrayList<ChooseBean> stateList;
    private String[] state = {"全部", "未完成", "进行中", "已完成"};

    private void initPopupWindow(View data, ArrayList<ChooseBean> popList) {
        View view = View.inflate(mContext, R.layout.layout_popup_hint, null);
        RecyclerView recyclerPop = view.findViewById(R.id.recyclerPop);
        recyclerPop.setLayoutManager(new LinearLayoutManager(mContext));
        SourceZAdapter sourceAdapter = new SourceZAdapter(mContext, popList);
        recyclerPop.setAdapter(sourceAdapter);
        popupWindow = new PopupWindow(view, 300, ViewGroup.LayoutParams.WRAP_CONTENT);

        view.setBackgroundColor(mContext.getResources().getColor(R.color.tv6060));
        // 设置PopupWindow的遮罩层颜色
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        // 点击框外可以使得popupwindow消失
        popupWindow.setFocusable(true);
        //设置可以触摸弹出框以外的区域
        popupWindow.setOutsideTouchable(false);
        popupWindow.setTouchable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());

        // 获取右侧控件的位置
        int[] location = new int[2];
        data.getLocationOnScreen(location);
        // 计算PopupWindow的显示位置
        int xPos = location[0] + data.getWidth(); // 水平位置从控件右侧开始
        int yPos = location[1] + data.getHeight(); // 垂直位置从控件下方开始

        popupWindow.showAtLocation(data, Gravity.NO_GRAVITY, xPos, yPos);
//        popupWindow.showAtLocation(data, Gravity.RIGHT, 0, 0);

        sourceAdapter.setOnItemClickListener((position, string) -> {
            stateList.get(0).setType(position);

            if (dataList.size() > 0) {
                if (position == 0) {
                    historyAdapter.setData(dataList);
                } else {
                    ArrayList<Map> mList = new ArrayList<>();
                    for (int i = 0; i < dataList.size(); i++) {
                        //1：未完成，2：进行中，3：已完成
                        int type = Integer.valueOf(dataList.get(i).get("type") + "");
                        if (type == position) {
                            mList.add(dataList.get(i));
                        }
                    }

                    historyAdapter.setData(mList);
                }
            }

            dismiss();
        });
    }

    private void dismiss() {
        if (popupWindow != null) {
            popupWindow.dismiss();
        }
    }

    private void getHistoryById(String id) {
    }

}
